package com.projetoptymo;


import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class ArretsProcheActivity extends Activity implements OnClickListener
{
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.arretprochelayout);
		
		Spinner spin1 = (Spinner) findViewById(R.id.Spinner03);
		
		
		SharedPreferences pref = getSharedPreferences("settings", MODE_PRIVATE);
		
		// on prend les X arrets les plus proches
		Map<Integer, BusStop> Arrets = BusStopList.getInstance().findNearestStop(CurrentPosition.getInstance().getPosition(), pref.getInt("busstop_count", 5));
		
		if(Arrets.isEmpty())
		{
			BusStop bs = new BusStop(null, "Position GPS indisponible", "");
			Arrets.put(0, bs);
			
		} else {
			Button btnSuite = (Button) findViewById(R.id.clock);
			
			btnSuite.setOnClickListener(this);
		}
		
		BusStop[] busarray = (BusStop[]) Arrets.values().toArray(new BusStop[Arrets.size()]);
		String[] strarray = new String[Arrets.size()];
		
		for(int i = 0; i<Arrets.size(); i++)
		{
			strarray[i] = busarray[i].getNom();
		}
		
		busarray = null;
		System.gc();
		
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item,
		        strarray);
		
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		spin1.setAdapter(spinnerArrayAdapter);
	}
	
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.clock:
		{
			// on reinit les params
			SharedPreferences pref = getSharedPreferences("settings", MODE_PRIVATE);
			SharedPreferences.Editor editor = pref.edit();
			
			// on reinit les params.
			editor.putBoolean("ChooseDepart", true);
			editor.putString("DepartNom", "");
			
			editor.commit();
			
			Intent j = new Intent(this, DestinationActivity.class);
			
			Bundle objetbunble = new Bundle();
			
			Spinner spin1 = (Spinner) findViewById(R.id.Spinner03);
			
			objetbunble.putString("DepartNom", (String)spin1.getSelectedItem());
	        j.putExtras(objetbunble);
			
			startActivity(j);
		}
		}
	}
	
}
